﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;

namespace THO7AlgorithmBase
{
    /// <summary>
    /// This class may be used to implement an OCR Algorithm
    /// </summary>
    public abstract partial class OCRAlgorithm : THO7AlgorithmBase.Algorithm
    {
        public OCRAlgorithm(String name)
            : base(name)
        {
        }
        /// <summary>
        /// Calls DoOCR and returns the character with the highest score
        /// </summary>
        /// <param name="sourceImage"></param>
        /// <returns></returns>
        public Char DoOCRAndGetCandidate(Bitmap sourceImage){
            double highestValue = 0;
            char highestLetter = '!';
            Double[] results = DoOCR(sourceImage);

            for (int i = 0; i < results.Length; i++)
            {
                if (results[i] > highestValue)
                {
                    highestValue = results[i];
                    highestLetter = indexToCharacter(i);
                }
            }
            
            return highestLetter;
            //Determine the best match, return that character
        }
        
        /// <summary>
        /// Does the OCR algorithm
        /// </summary>
        /// /// <param name="sourceImage">The source image containing ONE character</param>
        /// <returns>An array of Doubles. These represent the percentage of matches with known letters.</returns>
        public abstract Double[] DoOCR(Bitmap sourceImage);

        //TJERK HAX BEYOND THIS POINT
        private int characterToIndex(String s) //actually a string with length 1, I know...
        {
            int arrayIndex = 0;
            int asciiValue = Encoding.ASCII.GetBytes(s)[0];
            if ((asciiValue < 58) && (asciiValue > 47)) //0-9
            {
                arrayIndex = asciiValue - 48;
            }
            else if ((asciiValue < 91) && (asciiValue > 64)) //A-Z
            {
                arrayIndex = (asciiValue - 65) + 10;
            }
            return arrayIndex;
        }
        private char indexToCharacter(int asciiValue)
        {
            if (asciiValue < 10) //0-9
            {
                return (char)(asciiValue + 48);
            }
            else if (asciiValue > 9) //A-Z
            {
                return(char)(asciiValue + 55); 
            }
            return '!';
        }
        //END OF TJERK HAX
    }


}